from typing import *

class Solution:
    def wiggleMaxLength2(self, nums: List[int]) -> int:
        flag = None
        ans = 1
        def sign(e):
            if e > 0: return True
            elif e < 0: return False
            else: return None

        for idx in range(1, len(nums)):
            tmp_flag = sign(nums[idx]-nums[idx-1])
            if flag is None and tmp_flag is not None:
                flag = tmp_flag
                ans = 2
            else:
                if tmp_flag is not None and tmp_flag is not flag:
                    ans += 1
                    flag = tmp_flag
        return ans

    def wiggleMaxLength(self, nums: List[int]) -> int:
        maxlen = 0
        if len(nums) < 2:
            return len(nums)
        if len(nums) == 2:
            return 1 if nums[1] == nums[0] else 2
        
        def sign(e):
            if e > 0: return True
            elif e < 0: return False
            else: return None

        idx = 1
        ans = 1
        flag = None
        while idx < len(nums):
            tmp_flag = sign(nums[idx]-nums[idx-1])
            if tmp_flag is not None:
                if flag is None:
                    flag = tmp_flag
                    ans = 2
                    continue
                if tmp_flag is not flag:
                    ans += 1
                    flag = tmp_flag
            idx += 1
        return ans

if __name__ == "__main__":
    print('''#376. 摆动序列''')
    print("https://leetcode.cn/problems/wiggle-subsequence/description/")
    nums = [1,17,5,10,13,15,10,5,16,8]
    #nums = [0, 1, 0, 0]
    print(nums)
    ret = Solution().wiggleMaxLength(nums)
    print(ret)
    ret = Solution().wiggleMaxLength2(nums)
    print(ret)